<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:ci="http://apache.org/cocoon/include/1.0" xmlns:cu="http://apache.org/cocoon/counit"
  xmlns:source="http://apache.org/cocoon/source/1.0"
  xmlns:map="http://apache.org/cocoon/sitemap/1.0" version="1.0">

  <!--
    Prepare a transformer element by constructing the sub-sitemap and instruct the sourcewriter
    to write it some place, and by constructing the cinclude to call this subpipeline (indirectly, 
    via the main sitemap).
  -->

  <xsl:include href="cook-cinclude.xsl"/>

  <xsl:param name="timestamp"/>
  <xsl:param name="sitemap-uri-prefix"/>

  <!-- 
    copy non-transformer testcases directly to the result tree
    to prevent processing of elements that should be processed recursively.-->
  <xsl:template match="cu:testcase[not(cu:transformer)]">
    <xsl:copy-of select="."/>
  </xsl:template>


  <!-- add a type attribute to the testcase -->
  <xsl:template match="cu:testcase[cu:transformer]">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:attribute name="type">transformer</xsl:attribute>
      <xsl:apply-templates/>
    </xsl:copy>
  </xsl:template>

  <!-- skip the cu:generate element (a document element to hold the transformation
    in case of a generated stylesheet) -->
  <xsl:template match="cu:generate">
    <xsl:choose>
      <xsl:when test="cu:transformer">
        <xsl:apply-templates/>
      </xsl:when>
      <xsl:otherwise>
        <!-- we have nothing to do with this one. copy it to the result tree -->
        <xsl:copy-of select="."/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <!-- copy ci elements, because these are processed already -->
  <xsl:template match="ci:*">
    <xsl:copy-of select="."/>
  </xsl:template>
  

  <xsl:template match="cu:transformer">
    <xsl:variable name="temp-sitemap">
      <xsl:text>temp_</xsl:text>
      <xsl:value-of select="generate-id(parent::cu:testcase)"/>
      <xsl:text>_</xsl:text>
      <xsl:value-of select="$timestamp"/>
      <xsl:text>.xmap</xsl:text>
    </xsl:variable>
    <!-- write the sitemap -->
    <source:write>
      <source:source>
        <!-- the sitemap will be written in the counit directory -->
        <xsl:value-of select="$temp-sitemap"/>
      </source:source>
      <source:fragment>
        <map:sitemap>
          <!-- include resource from the main sitemap -->
          <map:resources>
            <xsl:copy-of select="document('sitemap.xmap')//map:resource[@name='post-testcase']"/>
          </map:resources>
          <map:components>
            <map:transformers>
              <map:transformer name="counit-transformation-test" src="{@src}">
                <xsl:apply-templates select="cu:configuration/node()"/>
              </map:transformer>
            </map:transformers>
          </map:components>
          <map:pipelines>
            <map:pipeline>
              <map:match pattern="**">
                <!-- read input xml from the POST request -->
                <map:generate src="cocoon://{$sitemap-uri-prefix}read-input-xml"/>
                <map:transform type="counit-transformation-test">
                  <xsl:apply-templates select="cu:attribute"/>
                  <xsl:apply-templates select="cu:parameter"/>
                </map:transform>
                <map:call resource="post-testcase"/>
                <map:serialize type="xml"/>
              </map:match>
            </map:pipeline>
          </map:pipelines>
        </map:sitemap>
      </source:fragment>
    </source:write>
    <!-- write cinclude tag -->
    <xsl:call-template name="cook-cinclude">
      <xsl:with-param name="pipeline-to-call"
        select="concat('transform-by-transformer', '/', $temp-sitemap)"/>
    </xsl:call-template>
  </xsl:template>

  <!-- copy attributes -->
  <xsl:template match="cu:attribute[ancestor::cu:transformer]">
    <xsl:variable name="value">
      <xsl:choose>
        <xsl:when test="@value">
          <xsl:value-of select="@value"/>
        </xsl:when>
        <xsl:when test="@file">
          <xsl:value-of select="ancestor::cu:*[@base][1]/@base"/>
          <xsl:value-of select="ancestor::cu:*[@testsuite-dir][1]/@testsuite-dir"/>
          <xsl:value-of select="@file"/>
        </xsl:when>
      </xsl:choose>
    </xsl:variable>
    <xsl:attribute name="{@name}"><xsl:value-of select="$value"/></xsl:attribute>
  </xsl:template>

  <!-- copy parameters into the sitemap (and leave other parameters alone) -->
  <xsl:template match="cu:parameter[ancestor::cu:transformer]">
    <xsl:variable name="value">
      <xsl:choose>
        <xsl:when test="@value">
          <xsl:value-of select="@value"/>
        </xsl:when>
        <xsl:when test="@file">
          <xsl:value-of select="ancestor::cu:*[@base][1]/@base"/>
          <xsl:value-of select="ancestor::cu:*[@testsuite-dir][1]/@testsuite-dir"/>
          <xsl:value-of select="@file"/>
        </xsl:when>
      </xsl:choose>
    </xsl:variable>
    <map:parameter name="{@name}" value="{$value}"/>
  </xsl:template>

  <!-- no special local settings (called by the cook-cinclude template)-->
  <xsl:template match="cu:transformer" mode="local-settings"/>

  <!-- eat the cu:input element, which has been used already -->
  <xsl:template
    match="cu:input[preceding-sibling::cu:transformer or following-sibling::cu:transformer]"/>

</xsl:stylesheet>
